package com.heima.search.listener;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.common.constants.MQConstants;
import com.heima.search.service.ArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 文章上下架同步ES操作
 */
@Component
@Slf4j
public class ArticleDocumentListener {
    @Autowired
    private ArticleSearchService articleSearchService;

    @KafkaListener(topics = MQConstants.WM_NEW_UP_ES_TOPIC)
    public void handleUp(String articleId){
        log.info("接收到文章上架ES同步请求,ID :{}",articleId);
        if(StringUtils.isNotEmpty(articleId)){
            articleSearchService.saveToES(Long.valueOf(articleId));
        }
    }

    /**
     * 文章下架
     * @param articleId
     */
    @KafkaListener(topics = MQConstants.WM_NEW_DOWN_ES_TOPIC)
    public void handleDown(String articleId){
        log.info("接收到文章下架ES同步请求,ID :{}",articleId);
        if(StringUtils.isNotEmpty(articleId)){
            articleSearchService.removeFromES(Long.valueOf(articleId));
        }
    }
}
